package com.javaIO.demo1;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;

public class Test2 {
    public static void main(String[] args) {

        FileInputStream f = null;
        try {
            f = new FileInputStream("D:\\SoftDevelopment\\Java\\file\\demo1.txt");
            //从硬盘中每次读取一个字节数据到内存中
//            int read1 = f.read();
//            System.out.println((char) read1);//a
//            int read2 = f.read();
//            System.out.println((char) read2);//c
//            int read3 = f.read();
//            System.out.println((char) read3);//b

            //循环read文件内容   read = -1代表文件读取完毕
            System.out.println("读取文件内容为：");
//            int read = f.read();
//            while (read != -1) {
//                System.out.print((char) read);
//                read = f.read();
//            }
            //优化
            int read;
            while ((read = f.read()) != -1) {
                System.out.print((char) read);
            }

        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            if (f != null) {
                try {
                    f.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}
